package org.museum.collection.service.impl;

import org.museum.collection.domain.entity.Collection;
import org.museum.collection.domain.pojo.CustomCollection;
import org.museum.collection.domain.pojo.ListTableCollectionDetail;
import org.museum.collection.mapper.CollectionMapper;
import org.museum.collection.service.ICollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 藏品主表 服务实现类
 * </p>
 *
 * @author zafkiel
 * @since 2025-07-11
 */
@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collection> implements ICollectionService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    //获取点赞的lua脚本
    private static final DefaultRedisScript<Long> Collection_Like_Script;
    //获取取消点赞的lua脚本
    private static final DefaultRedisScript<Long> Collection_Cancel_Like_Script;
    //静态代码块
    static {
        Collection_Like_Script = new DefaultRedisScript<>();
        Collection_Like_Script.setLocation(new ClassPathResource("Collection_Like.lua"));
        Collection_Like_Script.setResultType(Long.class);
    }
    //静态代码块
    static {
        Collection_Cancel_Like_Script = new DefaultRedisScript<>();
        Collection_Cancel_Like_Script.setLocation(new ClassPathResource("Collection_Cancel_Like.lua"));
        Collection_Cancel_Like_Script.setResultType(Long.class);
    }
    @Override
    public ListTableCollectionDetail getCollectionDetail(String name) {
        return baseMapper.getCollectionDetail(name);
    }

    @Override
    public List<CustomCollection> getRandomCollection(String category) {
        return baseMapper.getRandomCollection(category);
    }

    @Override
    public boolean isLikeCollection(String phone, Integer collectionId) {
        return Boolean.TRUE.equals(stringRedisTemplate.opsForSet().isMember("collection:" + collectionId + ":liked_users", phone));
    }

    @Override
    public boolean likeCollection(String phone, Integer collectionId) {
        //执行lua点赞脚本
        Long result = stringRedisTemplate.execute(Collection_Like_Script,
                Collections.emptyList(),
                phone, String.valueOf(collectionId));
        if (result != null) {
            return result == 0;
        }
        return false;
    }

    @Override
    public boolean cancelLikeCollection(String phone, Integer collectionId) {
        //执行lua取消点赞脚本
        Long result = stringRedisTemplate.execute(Collection_Cancel_Like_Script,
                Collections.emptyList(),
                phone, String.valueOf(collectionId));
        if (result != null) {
            return result == 0;
        }
        return false;
    }
}
